<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-content">
        <div class="title">
          <h2>在线知识竞赛与排名系统</h2>
          <p class="subtitle">探索知识，挑战自我</p>
        </div>
        
        <el-form ref="loginFormRef" :model="loginForm" :rules="loginRules" class="login-form">
          <el-form-item prop="username">
            <el-input
              v-model="loginForm.username"
              placeholder="用户名"
              clearable
            >
              <template #prefix>
                <el-icon><user /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              placeholder="密码"
              clearable
              show-password
              type="password"
            >
              <template #prefix>
                <el-icon><lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          
          <div class="actions">
            <el-button class="register-btn" type="text" @click="showRegisterDialog = true">
              没有账号？点击注册
            </el-button>
          </div>
          
          <el-form-item class="login-btn-container">
            <el-button type="primary" round :loading="loading" @click="handleLogin">
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <!-- 注册对话框 -->
    <el-dialog
      v-model="showRegisterDialog"
      title="用户注册"
      width="460px"
      destroy-on-close
      center
      custom-class="register-dialog"
    >
      <register-form @register-success="handleRegisterSuccess" />
    </el-dialog>
  </div>
</template>

<script>
import { ref, reactive, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User as IconUser, Lock as IconLock } from '@element-plus/icons-vue'
import { login, getUserInfo } from '@/api/user'
import RegisterForm from './components/RegisterForm.vue'

export default {
  name: 'LoginView',
  components: {
    RegisterForm,
    user: IconUser,
    lock: IconLock
  },
  setup() {
    const router = useRouter()
    const loginFormRef = ref(null)
    const loading = ref(false)
    const showRegisterDialog = ref(false)
    
    // 登录表单
    const loginForm = reactive({
      username: '',
      password: ''
    })
    
    // 表单校验规则
    const loginRules = {
      username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 4, max: 20, message: '用户名长度必须在4-20个字符之间', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, max: 20, message: '密码长度必须在6-20个字符之间', trigger: 'blur' }
      ]
    }
    
    // 处理登录
    const handleLogin = () => {
      loginFormRef.value.validate(async (valid) => {
        if (valid) {
          loading.value = true
          try {
            // 1. 登录获取token
            const res = await login(loginForm)
            localStorage.setItem('token', res.data)
            
            // 2. 登录成功后获取用户信息
            const userRes = await getUserInfo()
            localStorage.setItem('userInfo', JSON.stringify(userRes.data))
            
            ElMessage.success('登录成功')
            router.push('/dashboard')
          } catch (error) {
            console.error('登录失败', error)
          } finally {
            loading.value = false
          }
        }
      })
    }
    
    // 处理注册成功
    const handleRegisterSuccess = () => {
      showRegisterDialog.value = false
      ElMessage.success('注册成功，请登录')
    }
    
    onMounted(() => {
      // 如果有token，直接跳转到首页
      const token = localStorage.getItem('token')
      if (token) {
        router.push('/dashboard')
      }
    })
    
    return {
      loginFormRef,
      loginForm,
      loginRules,
      loading,
      showRegisterDialog,
      handleLogin,
      handleRegisterSuccess
    }
  }
}
</script>

<style lang="scss" scoped>
.login-container {
  height: 100vh;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #3a7bd5, #00d2ff);
  overflow: hidden;
  position: relative;
  
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: url('@/assets/images/login-bg.png') no-repeat center center;
    background-size: cover;
    opacity: 0.5;
  }
  
  .login-box {
    background: rgba(255, 255, 255, 0.9);
    border-radius: 16px;
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.1);
    width: 420px;
    overflow: hidden;
    backdrop-filter: blur(10px);
    position: relative;
    z-index: 1;
    transition: all 0.3s ease;
    
    &:hover {
      box-shadow: 0 12px 40px rgba(0, 0, 0, 0.15);
      transform: translateY(-5px);
    }
    
    .login-content {
      padding: 40px;
      
      .title {
        text-align: center;
        margin-bottom: 30px;
        
        h2 {
          color: #3a7bd5;
          font-size: 28px;
          font-weight: 600;
          margin-bottom: 8px;
        }
        
        .subtitle {
          color: #718096;
          font-size: 16px;
        }
      }
      
      .login-form {
        .el-input {
          --el-input-hover-border-color: #3a7bd5;
          --el-input-focus-border-color: #3a7bd5;
          border-radius: 8px;
          
          :deep(.el-input__wrapper) {
            border-radius: 8px;
            padding: 0 15px;
            box-shadow: 0 0 0 1px #e2e8f0;
            height: 46px;
            
            &.is-focus {
              box-shadow: 0 0 0 1px #3a7bd5;
            }
          }
          
          :deep(.el-input__inner) {
            height: 46px;
          }
        }
        
        .login-btn-container {
          margin-top: 30px;
          
          .el-button {
            width: 100%;
            height: 46px;
            font-size: 16px;
            background: linear-gradient(135deg, #3a7bd5, #00d2ff);
            border: none;
            
            &:hover, &:focus {
              background: linear-gradient(135deg, #2d62ab, #00b8e6);
            }
          }
        }
        
        .actions {
          display: flex;
          justify-content: flex-end;
          margin-top: -10px;
          
          .register-btn {
            color: #3a7bd5;
            font-size: 14px;
            
            &:hover {
              color: #2d62ab;
            }
          }
        }
      }
    }
  }
}

// 自定义对话框样式
:deep(.register-dialog) {
  border-radius: 16px;
  
  .el-dialog__header {
    padding: 20px 20px 10px;
    text-align: center;
    
    .el-dialog__title {
      font-size: 20px;
      font-weight: 600;
      color: #3a7bd5;
    }
  }
  
  .el-dialog__body {
    padding: 20px 30px 30px;
  }
}
</style> 